﻿using CurvaLauncher.Services;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfAppFlyshoot
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        int slaveID = 0, chanelID = 0;

        DispatcherTimer dispatcherTimer;
        public I18nService I18nService;
        public MainWindow()
        {
            InitializeComponent();

            dispatcherTimer = new DispatcherTimer(DispatcherPriority.Background);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
            dispatcherTimer.Tick += DispatcherTimer_Tick;
            dispatcherTimer.IsEnabled = false;


        I18nService = new I18nService();
        I18nService.ApplyLanguage(AppLanguage.Auto); ;
        I18nService.CurrentCultureChanged += I18NServiceCurrentCultureChanged;
        }

        private void I18NServiceCurrentCultureChanged(object sender, EventArgs e)
        {
           
        }

        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            int pos = 0;
            FlyShoot.Controller.GetEncoderPosition(this.slaveID, this.chanelID, ref pos);
            this.txbkCurrentPos.Text = pos.ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int slaveCount = 0;
            Task.Run(() =>
            {
                FlyShoot.Controller.Initial(ref slaveCount);
            }).Wait();
            if (slaveCount > 0)
            {
                for (int i = 1; i <= slaveCount; i++)
                {
                    this.comboBoxSlaveID.Items.Add(i.ToString());
                }
                dispatcherTimer.IsEnabled = true;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            int pos = 0;
            int.TryParse(this.txbEncoder.Text, out pos);
            FlyShoot.Controller.SetEncoderPosition(this.slaveID, this.chanelID, pos);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            FlyShoot.Controller.SetTrigParam(this.slaveID, this.chanelID);
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            FlyShoot.Controller.ManualOut(this.slaveID, this.chanelID);
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            FlyShoot.Controller.Start(this.slaveID, this.chanelID);
            this.btnStart.IsEnabled = false;
            this.btnStop.IsEnabled = true;
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            FlyShoot.Controller.Stop(this.slaveID, this.chanelID);
            this.btnStart.IsEnabled = true;
            this.btnStop.IsEnabled = false;
        }

        private void comboBoxSlaveID_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.slaveID = int.Parse(this.comboBoxSlaveID.SelectedValue.ToString());

        }

        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            string s = this.txbTrigData.Text.Trim();
            string[] strDatas = s.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            if (strDatas.Length > 0)
            {
                int[] trigDatas = new int[strDatas.Length];
                for (int i = 0; i < trigDatas.Length; i++)
                {
                    trigDatas[i] = int.Parse(strDatas[i]);
                }
                FlyShoot.Controller.SetTrigData(this.slaveID, this.chanelID, trigDatas);
            }
        }

        private void comboBoxChanelID_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string s = this.comboBoxChanelID.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem:", "");
            this.chanelID = int.Parse(s);
        }


        private void Language_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            I18nService.ApplyLanguage((AppLanguage)Language.SelectedIndex); 
        }
    }
}
